package com.lz.sports.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.lz.library.dialog.BaseDialogFragment
import com.lz.library.extension.APP
import com.lz.library.extension.click
import com.lz.library.extension.getScreenPoint
import com.lz.sports.R
import com.lz.sports.databinding.DialogSignatureBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//电子签名
class SignatureDialog : BaseDialogFragment<DialogSignatureBinding>() {

    companion object {
        fun show(activity: FragmentActivity, path: String): SignatureDialog {
            val dialog = SignatureDialog().also {
                it.arguments = Bundle().apply { putString("path", path) }
            }
            dialog.setFrom(true)
            dialog.show(activity.supportFragmentManager)
            return dialog
        }

        fun show(fragment: Fragment, path: String): SignatureDialog {
            val dialog = SignatureDialog().also {
                it.arguments = Bundle().apply { putString("path", path) }
            }
            dialog.show(fragment.childFragmentManager)
            return dialog
        }
    }

    override fun getLayoutRes(): Int = R.layout.dialog_signature

    override fun initData(v: View, savedState: Bundle?) {
        lifecycleScope.launch {
            arrayOf(binding.tvSure, binding.tvCancel, binding.tvReset).click().collect {
                when (it.id) {
                    R.id.tv_cancel -> dismiss()
                    R.id.tv_sure -> {
                        val path = arguments!!.getString("path")!!
                        val res = binding.svSign.save(path)
                        if (isFromActivity()) {
                            if (context is OnSignatureSureListener) {
                                (context as OnSignatureSureListener).onSure(this@SignatureDialog, res, path)
                            }
                        } else {
                            if (parentFragment is OnSignatureSureListener) {
                                (parentFragment as OnSignatureSureListener).onSure(this@SignatureDialog, res, path)
                            }
                        }
                    }
                    R.id.tv_reset -> {
                        binding.svSign.clear()
                    }
                }
            }
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = object: BaseDialog(requireContext()) {
        override fun initParams(params: WindowManager.LayoutParams) {
            val screen = APP.getScreenPoint()
            params.width = (screen.x * 0.92f).toInt()
            params.height = (screen.y * 0.8f).toInt()
        }
    }

    interface OnSignatureSureListener {

        fun onSure(dialog: SignatureDialog, status: Boolean, path: String)
    }
}